package org.transfer.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.transfer.dao.RegisterTableDao;
import org.transfer.model.FinanceTemplate;
import org.transfer.model.RegisterTable;

@Transactional
@Service
public class RegisterTableService {
	@Autowired
	private RegisterTableDao registerTableDao;

	/**
	 * 获取单位登记表信息
	 * 
	 * @param registerTable
	 * @return
	 */
	public RegisterTable getTable(RegisterTable registerTable) {
		registerTable = registerTableDao.getTable(registerTable);
		return registerTable;
	}

	/**
	 * 获取模板列表
	 * 
	 * @param name
	 * @return
	 */
	public List<FinanceTemplate> findTemplateList(@Param("name") String name, @Param("dbType") String dbType) {
		return registerTableDao.findTemplateList(name, dbType);
	}

	public String findFinanceName(RegisterTable registerTable) {
		List<RegisterTable> financeList = registerTableDao.findFinanceList(registerTable);
		if (financeList.size() == 0) {
			return "";
		}
		String sysName = "";
		for (int i = 0; i < financeList.size(); i++) {
			RegisterTable temp = financeList.get(i);
			if (temp.getDataType().contains("财务") && temp.getDataName() != null
					&& (temp.getDataName().contains("财") || temp.getDataName().contains("账"))) {
				sysName = temp.getSysName();
				break;
			}
		}
		// 没找到最合适的,就取第一个
		if (StringUtils.isBlank(sysName)) {
			for (int i = 0; i < financeList.size(); i++) {
				RegisterTable temp = financeList.get(i);
				if (temp.getDataType().contains("财务") && temp.getSysName() != null) {
					sysName = temp.getSysName();
					break;
				}
			}
		}
		return sysName;
	}

	// 基础表_财务类_电子账簿信息表
	public int insertAccountBook(List<Map<String, Object>> list) {
		return registerTableDao.insertAccountBook(list);
	}

	// 基础表_财务类_辅助信息表
	public int insertAssist(List<Map<String, Object>> list) {
		return registerTableDao.insertAssist(list);
	}

	// 基础表_财务类_辅助余额表
	public int insertAssistBalance(List<Map<String, Object>> list) {
		return registerTableDao.insertAssistBalance(list);
	}

	// 基础表_财务类_会计科目表
	public int insertAccount(List<Map<String, Object>> list) {
		return registerTableDao.insertAccount(list);
	}

	// 基础表_财务类_记账凭证表
	public int insertVoucher(List<Map<String, Object>> list) {
		return registerTableDao.insertVoucher(list);
	}

	// 基础表_财务类_记账凭证辅助明细表
	public int insertAssistDetail(List<Map<String, Object>> list) {
		return registerTableDao.insertAssistDetail(list);
	}

	// 基础表_财务类_科目余额及发生额表
	public int insertAccountBalance(List<Map<String, Object>> list) {
		return registerTableDao.insertAccountBalance(list);
	}

	public int deleteFinanceTables(RegisterTable registerTable) {
		registerTableDao.deleteFinanceTables(registerTable);
		return registerTableDao.deleteAccountBook(registerTable);
	}

}
